<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
	<title>Upload File</title> 
	<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
	 <style type="text/css">
	body{
		margin: 1em;
	}
	</style>
</head> 
 
<body>
<?php
	$error = "";
	$msg = "";
	$img_field = 'attachment';
	$valid_types = array("application/pdf", "text/rtf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "text/plain");
	if(!empty($_FILES[$img_field]['error']))
	{
		switch($_FILES[$img_field]['error'])
		{

			case '1':
				$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
				break;
			case '2':
				$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
				break;
			case '3':
				$error = 'The uploaded file was only partially uploaded';
				break;
			case '4':
				$error = 'No file was uploaded.';
				break;

			case '6':
				$error = 'Missing a temporary folder';
				break;
			case '7':
				$error = 'Failed to write file to disk';
				break;
			case '8':
				$error = 'File upload stopped by extension';
				break;
			case '999':
			default:
				$error = 'No error code avaiable';
		}
	}elseif(empty($_FILES[$img_field]['tmp_name']) || $_FILES[$img_field]['tmp_name'] == 'none')
	{
		$error = 'No file was uploaded..';
	}else 
	{
			$tmp_name = $_FILES[$img_field]["tmp_name"];
			$name = $_FILES[$img_field]['name'];
			$type = $_FILES[$img_field]['type'];
			
			$msg .= " File Name: " . $name . ", ";
			$msg .= "<br> File Size: " . @filesize($_FILES[$img_field]['tmp_name']);
			$msg .= "<br> File Size2: " . $_FILES[$img_field]['size'];
			$msg .= "<br> File Mime: " . $type;
			$msg .= "<br> Stored at: ".$tmp_name;
			// $msg .= "<br> overwrite: ".$_REQUEST['overwrite'];
			
			if (! in_array($type, $valid_types)){
				$error = "Invalid attachment type! (Valid types are: ";
				foreach ($valid_types as $t) {
					$error .= '<em>'.$t.'</em>, ';
				}
				')';
			}else{
			
			//todo: handle duplicate names:
			if (file_exists('../../../assets/news/'.$name)){
				$error = "<br>Aborted, file (<em>$name</em>) already exists!<br>";
				$_POST['overwrite'] = 'true';
				// $error .= '<br><button>Use Previous</button><button onclick="window.location.replace(window.location + \'?overwrite=true\');return false;">Overwrite File</button>';  //this loses file data in favor of tmp I believe.
				//todo: have these submit another request, pull tmp file, do something with it
				// $error .=  '<br><button disabled>Overwrite</button> <button disabled>Use Previous</button> <button disabled>Abort</button> ';
				$error .= "<br>Please rename your file and try again";
			}else{
				move_uploaded_file($tmp_name, '../../../assets/news/'.$name);
			}
		}
			

	}		
	if (empty($error)){
		echo "File (<em>$name</em>) uploaded successfully!";
		echo		"<br><b>info</b>:<br> " . $msg . "\n";
		$path = '<br><em>http://www.swett.com/assets/news/'.rawurlencode($name).'</em><br>';
	}else{
		echo		"<b>error:</b> " . $error . "\n";
		echo		"<br><br><b>info</b>:<br> " . $msg . "\n";
	}
	
?>
<br><br><button onclick="window.close();">Close window</button>

<?php if (empty($error)) { ?>
	
<script type="text/javascript">
a_displayer = window.opener.document.getElementById('attachments_display');
a_field = window.opener.document.getElementById('attachments_field');
btn = window.opener.document.getElementById('upload_attachment_btn');
var oldHtml = a_displayer.innerHTML, oldVal = a_field.value;
a_displayer.innerHTML = oldHtml + '<?php echo $path ?>';
a_field.value = oldVal + ", <?php echo $name ?>";
console.log(a_field.value);
btn.innerHTML = "Add Another";
window.close();

//fail at using #
// console.log(window.opener.j);
// jj = window.opener.jQuery.noConflict();
// console.log(jj("attachments_display"));
// console.log(window.opener.j("attachments_display"));
// jj("attachments_display").append("<br><em>http://www.swett.com/assets/news/<?php echo  $name; ?></em>");
// oldVal = window.opener.j("attachments_field").val();
// window.opener.j("attachments_field").val(oldVal + ",<?php $name ?>");
// window.opener.j('upload_attachment_btn').val('Add another');

//	echo "window.close();";
	</script>
	
<?php } ?>
</body> 
</html>